package com.bree.erp.report.birt;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import org.eclipse.birt.report.engine.api.EngineConfig;
import org.eclipse.birt.report.engine.api.EngineConstants;
import org.eclipse.birt.report.engine.api.EngineException;
import org.eclipse.birt.report.engine.api.IRenderOption;
import org.eclipse.birt.report.engine.api.IReportEngine;
import org.eclipse.birt.report.engine.api.IReportRunnable;
import org.eclipse.birt.report.engine.api.IRunAndRenderTask;
import org.eclipse.birt.report.engine.api.RenderOption;
import org.eclipse.birt.report.engine.api.ReportEngine;

import com.bree.erp.report.birt.param.PurchaseOrderFormParam;
import com.bree.erp.report.entity.po.PurchaseOrderDataSet;

public class Birt {
	
	public static final String REPORTWS = "D:\\MyJava\\eclipse\\luna\\workspace\\Br.ee.report\\src\\main\\resources";
	
	@SuppressWarnings("unchecked")
	public static void pdf(InputStream is, OutputStream os, PurchaseOrderDataSet ds) throws EngineException {

		IReportEngine engine;
        EngineConfig config = new EngineConfig();
        engine = new ReportEngine(config);

        final IReportRunnable design = engine.openReportDesign(is);

        // Create task to run and render the report,
        final IRunAndRenderTask task = engine.createRunAndRenderTask(design);
        
        // Set parent classloader for engine
        task.getAppContext().put(EngineConstants.APPCONTEXT_CLASSLOADER_KEY,
        		Birt.class.getClassLoader());
        
        task.getAppContext().put("purchaseOrderDataSet", ds);

        final IRenderOption options = new RenderOption();
        options.setOutputFormat("pdf");

        options.setOutputStream(os);
	}

	public static void main(String ... argv) throws EngineException, FileNotFoundException {
		 OutputStream os = new FileOutputStream(REPORTWS + "\\PurchaseOrder.pdf");
		 
		 PurchaseOrderFormParam param = new PurchaseOrderFormParam();
		 param.setSupplierAddress("Owen Bringino\nB10 L10 Cerritos East Camella Homes\nSan Miguel, Pasig City");
		 param.setWarehouseAddress("Bryant Cledera\n15F Bonifacio Technology Tower 1\nBGC, Taguig City");
		 param.setDueDate("15 May 2015");
		 param.setPoNumber("PR-00003");
		 param.setPurchaseDate("7 May 2015");
		 param.setPurchaser("Bryant Cledera");
		 param.setTerms("30 days");
		 param.setTotalAmount(56850.25);
		 param.setRemarks("1.) Intended for JOB#53\n2.) FUJIMOLD machine panel installation");
		
		 new PurchaseOrderForm(param).render(os);
	}
	
	public static void main1(String ... argv) throws EngineException, FileNotFoundException {
		
		IReportEngine engine;
        EngineConfig config = new EngineConfig();
        engine = new ReportEngine(config);
        
	
        final IReportRunnable design = engine.openReportDesign(REPORTWS + "\\PurchaseOrder.rptdesign");
        OutputStream os = new FileOutputStream(REPORTWS + "\\PurchaseOrder.pdf");
              
        // Create task to run and render the report,
        final IRunAndRenderTask task = engine.createRunAndRenderTask(design);

        // Set parent classloader for engine
        task.getAppContext().put(EngineConstants.APPCONTEXT_CLASSLOADER_KEY,
        		Birt.class.getClassLoader());

        final IRenderOption options = new RenderOption();
        options.setOutputFormat("pdf");

        options.setOutputStream(os);
        
        //final PDFRenderOption pdfOptions = new PDFRenderOption(options);

        task.setRenderOption(options);

        // run and render report
        task.run();
        task.close();
        
        
	}

}
